package action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import entitybean.l_sequence.EntityBeanL_Sequence;
import entitybean.oc_boat.EntityBeanOc_Boat;
import entitybean.oc_boat_ben.EntityBeanOc_Boat_Ben;
import entitybean.oc_sea_area.EntityBeanOc_Sea_Area;
import entitybean.oc_sea_area_ben.EntityBeanOc_Sea_Area_Ben;
import entitybean.oc_station.EntityBeanOc_Station;
import entitybean.oc_station_ben.EntityBeanOc_Station_Ben;
import entitybean.oc_voyage.EnitityBeanOc_Voyage;
import entitybean.oc_voyage_ben.EnitityBeanOc_Voyage_Ben;
import form.StationBenForm;
import form.StationForm;

import service.service_admin.Service_Admin;
import service.service_public.Service_Public;
import tools.Convert;
import tools.InterfaceForResultSetToList;
import tools.PageBeanPlus;
import view.ViewOc_Station;
import view.ViewOc_Station_Ben;

public class Action_Oc_Station_Ben extends DispatchAction {
	private Service_Admin service_admin;
	private Service_Public service_public;
	
	public Service_Admin getService_admin() {
		return service_admin;
	}

	public void setService_admin(Service_Admin serviceAdmin) {
		service_admin = serviceAdmin;
	}

	public Service_Public getService_public() {
		return service_public;
	}

	public void setService_public(Service_Public servicePublic) {
		service_public = servicePublic;
	}

	public ActionForward admin_init_oc_station_ben(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StationBenForm stationbenform = (StationBenForm) form;
		System.out.println("初始化海洋环境科考船");
		String sql = "select ID,STATION_NAME,LONGITUDE,DIMENSIONALITY,TERRITORIAL_WATERS,WORK_TIME,DEPTH,OC_VOYAGE_ID," +
				"(SELECT OC_SEA_AREA from OC_SEA_AREA_BEN where id=TERRITORIAL_WATERS)OC_SEA_AREA,"+
"(SELECT VOYAGE_NAME FROM OC_VOYAGE_BEN WHERE ID=OC_VOYAGE_ID)VOYAGE_NAME from OC_STATION_BEN where DEL='OFF'";
		System.out.println("sql="+sql);
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(stationbenform.getRowcountperpage()), Integer
						.valueOf(stationbenform.getCurrentpage()),
				"loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Station_Ben> list = interfaceforresultsettolist
				.ViewOc_Station_Ben(pagebeanplus.getFormatqueryresultset());
		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("stationbenform", stationbenform);
		System.out.println("list.size()" + list.size());
		
		
		
		return mapping.findForward("success");
	}

	public ActionForward admin_load_oc_station_ben(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StationBenForm stationbenform = (StationBenForm) form;
		System.out.println("封装表格");
		List<EntityBeanOc_Sea_Area_Ben> list_sea_area=this.service_admin.Find_Oc_Sea_Area_Ben_ISACTIVE();
		List<EntityBeanOc_Boat_Ben> list_boat=this.service_admin.Find_Oc_Boat_Ben_By_oc_boatkind(8);
		String id=stationbenform.getId();
		EntityBeanOc_Station_Ben e=new EntityBeanOc_Station_Ben();
		e=	this.service_admin.Find_Oc_Station_Ben_by_oc_id(Integer.valueOf(id));
	
		String ns="";
		String ew="";
		String a[]=e.getLongitude().split(" ");
		String b[]=e.getDimensionality().split(" ");
		ew=a[0];
		ns=b[0];
		e.setLongitude(a[1]);
		e.setDimensionality(b[1]);
		stationbenform.setEw(ew);
		stationbenform.setNs(ns);
		//配置相应的数据航次数据
		//e.getOc_voyage_id();//反查航次所属的科考船
		EnitityBeanOc_Voyage_Ben voy_ben=this.service_admin.Find_Oc_Voyage_Ben_By_id(e.getOc_voyage_id().toString());
		
		
		
		EntityBeanOc_Boat_Ben boat_ben=this.service_admin.Find_Oc_Boat_Ben_By_oc_boatkind_By_id(voy_ben.getBoatid().toString());
		List <EnitityBeanOc_Voyage_Ben>voyage_ben_list=this.service_admin.Find_Oc_Voyage_Ben_By_boatid(boat_ben.getId());
		
		request.setAttribute("boat", boat_ben);
		request.setAttribute("voyage_list", voyage_ben_list);
		
		request.setAttribute("e", e);
		request.setAttribute("list_sea_area", list_sea_area);
		request.setAttribute("list_boat", list_boat);
		request.setAttribute("stationbenform", stationbenform);
		
		
		
		
		return mapping.findForward("success");
	}

	public ActionForward admin_add_oc_station_ben(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		StationBenForm stationbenform = (StationBenForm) form;
		System.out.println("封装表格");
		List<EntityBeanOc_Sea_Area_Ben> list_sea_area=this.service_admin.Find_Oc_Sea_Area_Ben_ISACTIVE();
		List<EntityBeanOc_Boat_Ben> list_boat=this.service_admin.Find_Oc_Boat_Ben_By_oc_boatkind(8);
		
		
		//所属航次
		//所属海域
		//所属科考船
		
		
		request.setAttribute("list_sea_area", list_sea_area);
		request.setAttribute("list_boat", list_boat);
		
		
		
		
		
		
		return mapping.findForward("success");
	}

	public ActionForward admin_insert_oc_station_ben(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StationBenForm stationbenform = (StationBenForm) form;
		String ctd=stationbenform.getCtd();
		Double depth=stationbenform.getDepth();
		String dimensionality=stationbenform.getDimensionality();
		String longitude=stationbenform.getLongitude();
		Integer oc_voyage_id=stationbenform.getOc_voyage_id();
		Integer territorial_waters=stationbenform.getTerritorial_waters();
		String work_time=stationbenform.getWork_time();
		String	station_name=stationbenform.getStation_name();
		EntityBeanOc_Station_Ben e=new EntityBeanOc_Station_Ben();
		e.setCtd(ctd);
		e.setDel("OFF");
		e.setDepth(depth);
		e.setDimensionality(dimensionality);
		EntityBeanL_Sequence l = this.service_public
		.Find_L_Sequence_By_seq_Name("SEQ_OC_STATION_BEN");
		Integer id = l.getSeq_Value() + 1;
		l.setSeq_Value(id);
		
		e.setId(id);
		e.setLongitude(longitude);
		e.setOc_voyage_id(oc_voyage_id);
		e.setStation_name(station_name);
		e.setTerritorial_waters(territorial_waters);
		//work_time 转数据
		work_time=work_time+" 00:00:00";
		Convert convert=new Convert();
		e.setWork_time(convert.ConvertStringToDate(work_time));
		
		this.service_admin.Insert_Oc_Station_Ben(e);
		this.service_public.Update_L_Sequence(l);
		
		
		System.out.println("初始化海洋环境科考船");
		String sql = "select ID,STATION_NAME,LONGITUDE,DIMENSIONALITY,TERRITORIAL_WATERS,WORK_TIME,DEPTH,OC_VOYAGE_ID," +
				"(SELECT OC_SEA_AREA from OC_SEA_AREA_BEN where id=TERRITORIAL_WATERS)OC_SEA_AREA,"+
"(SELECT VOYAGE_NAME FROM OC_VOYAGE_BEN WHERE ID=OC_VOYAGE_ID)VOYAGE_NAME from OC_STATION_BEN where DEL='OFF'";
		System.out.println("sql="+sql);
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(stationbenform.getRowcountperpage()), Integer
						.valueOf(stationbenform.getCurrentpage()),
				"loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Station_Ben> list = interfaceforresultsettolist
				.ViewOc_Station_Ben(pagebeanplus.getFormatqueryresultset());
		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("stationbenform", stationbenform);
		System.out.println("list.size()" + list.size());
		
		
		return mapping.findForward("success");
	}

	public ActionForward admin_delete_oc_station_ben(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StationBenForm stationbenform = (StationBenForm) form;
		String id=stationbenform.getId();
		id=this.cut_tail(id);
		System.out.println("id="+id);
		this.service_admin.Delete_Oc_Station_Ben(id);
		
		
		System.out.println("初始化海洋环境科考船");
		String sql = "select ID,STATION_NAME,LONGITUDE,DIMENSIONALITY,TERRITORIAL_WATERS,WORK_TIME,DEPTH,OC_VOYAGE_ID," +
				"(SELECT OC_SEA_AREA from OC_SEA_AREA_BEN where id=TERRITORIAL_WATERS)OC_SEA_AREA,"+
"(SELECT VOYAGE_NAME FROM OC_VOYAGE_BEN WHERE ID=OC_VOYAGE_ID)VOYAGE_NAME from OC_STATION_BEN where DEL='OFF'";
		System.out.println("sql="+sql);
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(stationbenform.getRowcountperpage()), Integer
						.valueOf(stationbenform.getCurrentpage()),
				"loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Station_Ben> list = interfaceforresultsettolist
				.ViewOc_Station_Ben(pagebeanplus.getFormatqueryresultset());
		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("stationbenform", stationbenform);
		System.out.println("list.size()" + list.size());
		
		
		return mapping.findForward("success");
	}

	public ActionForward admin_update_oc_station_ben(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		StationBenForm stationbenform = (StationBenForm) form;
		String id=stationbenform.getId();
		String ctd=stationbenform.getCtd();
		Double depth=stationbenform.getDepth();
		String dimensionality=stationbenform.getDimensionality();
		String longitude=stationbenform.getLongitude();
		Integer oc_voyage_id=stationbenform.getOc_voyage_id();
		Integer territorial_waters=stationbenform.getTerritorial_waters();
		String work_time=stationbenform.getWork_time();
		String	station_name=stationbenform.getStation_name();
		EntityBeanOc_Station_Ben e=this.service_admin.Find_Oc_Station_Ben_by_oc_id(Integer.valueOf(id));
		e.setCtd(ctd);
		e.setDepth(depth);
		e.setDimensionality(dimensionality);
		e.setLongitude(longitude);
		e.setOc_voyage_id(oc_voyage_id);
		e.setStation_name(station_name);
		e.setTerritorial_waters(territorial_waters);
		//work_time 转数据
		work_time=work_time+" 00:00:00";
		Convert convert=new Convert();
		e.setWork_time(convert.ConvertStringToDate(work_time));
		
		this.service_admin.Update_Oc_Station_Ben(e);
		
		
		
		System.out.println("初始化海洋环境科考船");
		String sql = "select ID,STATION_NAME,LONGITUDE,DIMENSIONALITY,TERRITORIAL_WATERS,WORK_TIME,DEPTH,OC_VOYAGE_ID," +
				"(SELECT OC_SEA_AREA from OC_SEA_AREA_BEN where id=TERRITORIAL_WATERS)OC_SEA_AREA,"+
"(SELECT VOYAGE_NAME FROM OC_VOYAGE_BEN WHERE ID=OC_VOYAGE_ID)VOYAGE_NAME from OC_STATION_BEN where DEL='OFF'";
		System.out.println("sql="+sql);
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(stationbenform.getRowcountperpage()), Integer
						.valueOf(stationbenform.getCurrentpage()),
				"loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Station_Ben> list = interfaceforresultsettolist
				.ViewOc_Station_Ben(pagebeanplus.getFormatqueryresultset());
		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("stationbenform", stationbenform);
		System.out.println("list.size()" + list.size());
		
		
		return mapping.findForward("success");
	}

	public ActionForward admin_search_oc_station_ben(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
StationBenForm stationbenform = (StationBenForm) form;
		
		System.out.println("初始化海洋环境科考船");
		String sql = "select ID,STATION_NAME,LONGITUDE,DIMENSIONALITY,TERRITORIAL_WATERS,WORK_TIME,DEPTH,OC_VOYAGE_ID," +
				"(SELECT OC_SEA_AREA from OC_SEA_AREA_BEN where id=TERRITORIAL_WATERS)OC_SEA_AREA,"+
"(SELECT VOYAGE_NAME FROM OC_VOYAGE_BEN WHERE ID=OC_VOYAGE_ID)VOYAGE_NAME from OC_STATION_BEN where DEL='OFF'";
		System.out.println("sql="+sql);
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(stationbenform.getRowcountperpage()), Integer
						.valueOf(stationbenform.getCurrentpage()),
				"loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_Station_Ben> list = interfaceforresultsettolist
				.ViewOc_Station_Ben(pagebeanplus.getFormatqueryresultset());
		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("stationbenform", stationbenform);
		System.out.println("list.size()" + list.size());
		
		
		
		return mapping.findForward("success");
	}

	private String cut_tail(String str) {
		String cut = str;
		if (str.endsWith(",")) {
			cut = cut.substring(0, cut.length() - 1);
		}
		return cut;
	}
}
